package com.antaohua.tools.framework.base.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64ToImage {

	private String imagesBasePath;
	
	
	
	
	public String getImagesBasePath() {
		return imagesBasePath;
	}

	public void setImagesBasePath(String imagesBasePath) {
		if(this.imagesBasePath==null){
			this.imagesBasePath = imagesBasePath;
		}
	}

	
	
	
	private static Map<String, String> DATA_URI_SCHEME = new HashMap<>();
	static {
		Base64ToImage.DATA_URI_SCHEME.put("data:,", "txt");
		Base64ToImage.DATA_URI_SCHEME.put("data:text/plain,", "txt");
		Base64ToImage.DATA_URI_SCHEME.put("data:text/html", "html");
		Base64ToImage.DATA_URI_SCHEME.put("data:text/html;base64,", "html");
		Base64ToImage.DATA_URI_SCHEME.put("data:text/css,", "css");
		Base64ToImage.DATA_URI_SCHEME.put("data:text/css;base64,", "css");
		Base64ToImage.DATA_URI_SCHEME.put("data:text/javascript,", "js");
		Base64ToImage.DATA_URI_SCHEME.put("data:text/javascript;base64,", "js");
		Base64ToImage.DATA_URI_SCHEME.put("data:image/gif;base64,", "gif");
		Base64ToImage.DATA_URI_SCHEME.put("data:image/png;base64,", "png");
		Base64ToImage.DATA_URI_SCHEME.put("data:image/jpeg;base64,", "jpg");
		Base64ToImage.DATA_URI_SCHEME.put("data:image/x-icon;base64,", "icon");
	}

	/**
	 * @Descriptionmap 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
	 * @author temdy
	 * @Date 2015-01-26
	 * @param path
	 *            图片路径
	 * @return
	 */
	public String imageToBase64(String fileName) throws IOException {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		byte[] data = null;
		// 读取图片字节数组
		InputStream in = new FileInputStream(getImagesBasePath()+fileName);
		data = new byte[in.available()];
		in.read(data);
		in.close();
		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);// 返回Base64编码过的字节数组字符串
	}

	/**
	 * @Descriptionmap 对字节数组字符串进行Base64解码并生成图片
	 * @author temdy
	 * @Date 2015-01-26
	 * @param base64
	 *            图片Base64数据
	 * @param path
	 *            图片路径
	 * @return
	 */
	public String base64ToImage(String base64) throws IOException {// 对字节数组字符串进行Base64解码并生成图片
		String fileName = UUIDGenerator.getUUID();
		String lastName = null;
		for (Map.Entry<String, String> entry:Base64ToImage.DATA_URI_SCHEME.entrySet()) {
			if(base64.indexOf(entry.getKey())>-1){
				base64 = base64.trim();
				base64=base64.substring(entry.getKey().length(), base64.length()-1);
				base64 = base64.trim();
				lastName = entry.getValue();
				
			}
		}
		fileName = fileName + "."+lastName;
		BASE64Decoder decoder = new BASE64Decoder();

		// Base64解码
		byte[] bytes = decoder.decodeBuffer(base64);
		for (int i = 0; i < bytes.length; ++i) {
			if (bytes[i] < 0) {// 调整异常数据
				bytes[i] += 256;
			}
		}
		// 生成jpeg图片
		OutputStream out = new FileOutputStream(getImagesBasePath()+fileName);
		out.write(bytes);
		out.flush();
		out.close();
		return fileName;
	}

}
